package org.pay.shop.demo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.pay.core.common.util.PayDigestUtil;
import org.pay.core.common.util.PayUtil;

import java.util.Map;

/**
 * Created by yangshancheng on 17/10/30.
 */
public class RefundOrderDemo {

    // 商户ID
    static final String mchId = "20000027";//20001223,20001245
    static final String appId = "2e6c0342d38148db970585484b693b4e";
    // 商户私钥
    static final String privateKey = "NklfnwSUjJrnHkAsS9wCabrAZ6sGDz8EgHQtTfVPtePUugiI";

    //static final String baseUrl = "http://api.pay.org/api";
    static final String baseUrl = "http://localhost:3020/api";
    static final String notifyUrl = "http://127.0.0.1:8081/goods/notify_test?rt=success"; // 本地环境测试,可到ngrok.cc网站注册

    public static void main(String[] args) {
        //refundOrderTest();
        quryRefundOrderTest("REFUND1523860443997", "");
    }

    // 退款
    static String refundOrderTest() {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                               // 商户ID
        paramMap.put("appId", appId);
        paramMap.put("mchRefundNo", "REFUND" + System.currentTimeMillis());     // 商户订单号
        // 支付渠道ID, WX_NATIVE(微信扫码),WX_JSAPI(微信公众号或微信小程序),WX_APP(微信APP),WX_MWEB(微信H5),ALIPAY_WAP(支付宝手机支付),ALIPAY_PC(支付宝网站支付),ALIPAY_MOBILE(支付宝移动支付)
        paramMap.put("async", false);
        paramMap.put("amount", 1);  // 退款金额
        paramMap.put("currency", "cny");                            // 币种, cny-人民币
        paramMap.put("clientIp", "211.94.116.218");                 // 用户地址,微信H5支付时要真实的
        paramMap.put("device", "WEB");                              // 设备
        paramMap.put("subject", "PAY支付测试");
        paramMap.put("body", "PAY支付测试");
        //paramMap.put("notifyUrl", notifyUrl);                       // 回调URL
        paramMap.put("param1", "");                                 // 扩展参数1
        paramMap.put("param2", "");                                 // 扩展参数2
        paramMap.put("channelUser", "jmdhappy@126.com");  // 微信openId:oIkQuwhPgPUgl-TvQ48_UUpZUwMs(丁志伟)
        paramMap.put("payOrderId", "P01201804160258300470000");

        //{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}

        String reqSign = PayDigestUtil.getSign(paramMap, privateKey);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心退款接口,请求数据:" + reqData);
        String url = baseUrl + "/refund/create_order?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心退款接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode")) && "SUCCESS".equalsIgnoreCase(retMap.get("resCode").toString())) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, privateKey, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心退款验签成功=========");
            }else {
                System.err.println("=========支付中心退款验签失败=========");
                return null;
            }
        }
        return retMap.get("refundOrderId")+"";
    }

    static String quryRefundOrderTest(String mchRefundNo, String refundOrderId) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("mchId", mchId);                               // 商户ID
        paramMap.put("appId", appId);
        paramMap.put("mchRefundNo", mchRefundNo);                   // 商户退款单号
        paramMap.put("refundOrderId", refundOrderId);               // 退款单号
        paramMap.put("executeNotify", "true");                      // 是否执行回调,true或false,

        String reqSign = PayDigestUtil.getSign(paramMap, privateKey);
        paramMap.put("sign", reqSign);                              // 签名
        String reqData = "params=" + paramMap.toJSONString();
        System.out.println("请求支付中心查退款单接口,请求数据:" + reqData);
        String url = baseUrl + "/refund/query_order?";
        String result = PayUtil.call4Post(url + reqData);
        System.out.println("请求支付中心查退款单接口,响应数据:" + result);
        Map retMap = JSON.parseObject(result);
        if("SUCCESS".equals(retMap.get("retCode")) /*&& "SUCCESS".equalsIgnoreCase(retMap.get("resCode").toString())*/) {
            // 验签
            String checkSign = PayDigestUtil.getSign(retMap, privateKey, "sign");
            String retSign = (String) retMap.get("sign");
            if(checkSign.equals(retSign)) {
                System.out.println("=========支付中心查退款单验签成功=========");
            }else {
                System.err.println("=========支付中心查退款单验签失败=========");
                return null;
            }
        }
        return retMap.get("refundOrderId")+"";
    }

}
